//
//  LoginViewController.m
//  Trackit
//
//  Created by yaodaobin on 2017/4/4.
//  Copyright © 2017年 yaodaobin. All rights reserved.
//

#import "LoginViewController.h"
#import "TaskListViewController.h"


@interface LoginViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *usernameTextField;
@property (nonatomic, strong) UITextField *passwordTextField;
@property (nonatomic, strong) UIButton *loginBtn;
@property (strong, nonatomic) UIAlertControllerCompletionBlock tapBlock;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tapBlock = ^(UIAlertController *controller, UIAlertAction *action, NSInteger buttonIndex){
        if (buttonIndex == controller.destructiveButtonIndex) {
            NSLog(@"Delete");
        } else if (buttonIndex == controller.cancelButtonIndex) {
            NSLog(@"Cancel");
        } else if (buttonIndex >= controller.firstOtherButtonIndex) {
            NSLog(@"Other %ld", (long)buttonIndex - controller.firstOtherButtonIndex + 1);
        }
    };

    [self initParams];
    [self initUI];
}

- (void)initParams {
    [self setCustomTitle:NSLocalizedString(@"草畜信息采集", nil)];
}

- (void)initUI {
    kWeakSelf
    self.usernameTextField = [[UITextField alloc] init];
    self.usernameTextField.delegate = self;
    self.usernameTextField.placeholder = NSLocalizedString(@"用户名", nil);
    self.usernameTextField.textColor = [UIColor grayColor];
    [self.view addSubview:self.usernameTextField];
    
    UIView *usernameLine = [[UILabel alloc] init];
    usernameLine.backgroundColor = [UIColor greenColor];
    [self.view addSubview:usernameLine];
    
    self.passwordTextField = [[UITextField alloc] init];
    self.passwordTextField.delegate = self;
    self.passwordTextField.placeholder = NSLocalizedString(@"密码", nil);
    self.passwordTextField.textColor = [UIColor grayColor];
    self.passwordTextField.secureTextEntry = YES;
    [self.view addSubview:self.passwordTextField];
    
    UIView *passwordLine = [[UILabel alloc] init];
    passwordLine.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:passwordLine];
    
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.loginBtn setTitle:NSLocalizedString(@"登录", nil) forState:UIControlStateNormal];
    self.loginBtn.backgroundColor = [UIColor lightGrayColor];
    self.loginBtn.layer.cornerRadius = 2;
    [self.view addSubview:self.loginBtn];
    self.loginBtn.tappedBlock = ^(UIButton *btn) {
        [weakSelf loginAction];
    };
    
//    UILabel *zangLabel = [[UILabel alloc] init];
//    zangLabel.font = [UIFont systemFontOfSize:14];
//    zangLabel.textColor = [UIColor lightGrayColor];
//    zangLabel.text = @"藏语";
//    [self.view addSubview:zangLabel];
//
//    UISwitch *zangSwitch = [[UISwitch alloc] init];
//    zangSwitch.on = NO;
//    __block UISwitch *_zangSwitch = zangSwitch;
//    zangSwitch.tappedBlock = ^(UITapGestureRecognizer *tap) {
//        [UserDefaultManagement instance].showZangYu = _zangSwitch.on;
//
//    };
//    [self.view addSubview:zangSwitch];
    
    [self.usernameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(40);
        make.leading.offset(20);
        make.trailing.offset(-20);
        make.height.mas_equalTo(40);
    }];
    [usernameLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.usernameTextField.mas_bottom);
        make.leading.equalTo(weakSelf.usernameTextField.mas_leading);
        make.trailing.equalTo(weakSelf.usernameTextField.mas_trailing);
        make.height.mas_equalTo(1);
    }];

    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(usernameLine.mas_bottom).offset(20);
        make.leading.equalTo(weakSelf.usernameTextField.mas_leading);
        make.trailing.equalTo(weakSelf.usernameTextField.mas_trailing);
        make.height.equalTo(weakSelf.usernameTextField.mas_height);
    }];
    [passwordLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.passwordTextField.mas_bottom);
        make.leading.equalTo(weakSelf.passwordTextField.mas_leading);
        make.trailing.equalTo(weakSelf.passwordTextField.mas_trailing);
        make.height.equalTo(usernameLine.mas_height);
    }];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwordLine.mas_bottom).offset(50);
        make.leading.equalTo(weakSelf.usernameTextField.mas_leading);
        make.trailing.equalTo(weakSelf.usernameTextField.mas_trailing);
        make.height.mas_equalTo(40);
    }];
    
//    [zangLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(weakSelf.loginBtn.mas_bottom).offset(20);
//        make.leading.equalTo(weakSelf.usernameTextField.mas_leading);
//    }];
//    [zangSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(zangLabel.mas_trailing).offset(20);
//        make.centerY.equalTo(zangLabel.mas_centerY);
//    }];
}

#pragma mark - Private
- (void)resetUIWithZangSwitchAction {
    
}

- (void)loginAction {
    if (self.usernameTextField.text.length == 0) {
        [TKUtil showMessage:NSLocalizedString(@"用户名不能为空!", nil)];
        return;
    }
    if (self.passwordTextField.text.length == 0) {
        [TKUtil showMessage:NSLocalizedString(@"密码不能为空!", nil)];
        return;
    }
    kWeakSelf
    [self showWaitingIcon];
    self.loginBtn.enabled = NO;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic safeSetObject:self.usernameTextField.text forKey:@"username"];
    [dic safeSetObject:self.passwordTextField.text forKey:@"password"];
    [TrackitDataEngine appLoginAction:dic onComplete:^(NSDictionary *data, NSError *error) {
        [weakSelf dismissWaitingIcon];
        weakSelf.loginBtn.enabled = YES;
        if (data) {
            User *user = [[User alloc] initWithData:data[@"Data"]];
            user.password = weakSelf.passwordTextField.text;
            [user cleanOldCache];
            APP_DELEGATE.user = user;
            [UserDefaultManagement instance].user = user;
            [[UserDefaultManagement instance] saveLoginedUserID:user.userID];
            
            TaskListViewController *vc = [[TaskListViewController alloc] init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        } else {
            [TKUtil showMessage:error.userInfo[@"message"]];
        }
    }];
}

#pragma mark - UITextField
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}

@end
